package com.quanfaqiang.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.mapper.FurnitureMapper;
import com.quanfaqiang.demo1.mapper.FurnituretypesMapper;
import com.quanfaqiang.demo1.mapper.QuyutypesMapper;
import com.quanfaqiang.demo1.mapper.TaskMapper;
import com.quanfaqiang.demo1.pojo.Furniture;
import com.quanfaqiang.demo1.pojo.Furnituretypes;
import com.quanfaqiang.demo1.pojo.Quyutypes;
import com.quanfaqiang.demo1.pojo.Task;
import com.quanfaqiang.demo1.service.FurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Random;
import java.util.UUID;


@Service
public class FurnitureServiceImpl implements FurnitureService {
    @Autowired
    private FurnitureMapper furnitureMapper;
    @Autowired
    private QuyutypesMapper quyutypesMapper;
    @Autowired
    private FurnituretypesMapper furnituretypesMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Page<Furniture> SelectAll(int currentPage, int pageSize) {
        Page<Furniture> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        Page<Furniture> page1 = furnitureMapper.selectPage(page, queryWrapper);
        List<Furniture> records = page1.getRecords();
        for (Furniture record : records) {
            Quyutypes quyutypes = quyutypesMapper.selectById(record.getQuyuTypesId());
            record.setQuyuTypes(quyutypes.getName());
            Furnituretypes furnituretypes = furnituretypesMapper.selectById(record.getFurnitureTypesId());
            record.setFurnitureTypes(furnituretypes.getName());
            System.out.println(quyutypes + ":" + furnituretypes + "sssssssssssssssssssss");

        }
        return page1;
    }

    @Override
    public void DeleteId(Integer id) {
        taskMapper.delete(new QueryWrapper<Task>().eq("furniture_id",id));
        furnitureMapper.deleteById(id);
    }

    @Override
    public void deleteIds(List<Integer> ids) {
        for (Integer id : ids) {
            taskMapper.delete(new QueryWrapper<Task>().eq("furniture_id",id));
        }
        furnitureMapper.deleteBatchIds(ids);

    }

    @Override
    public Furniture SelectId(Integer id) {
        Furniture furniture = furnitureMapper.selectOne(new QueryWrapper<Furniture>().eq("id", id));
        return furniture;
    }

    @Override
    public void Add(Furniture furniture) {
//        Random rand = new Random();
//        // 生成一个范围在100000到999999之间的随机数
//        int sixDigitNumber = 100000 + rand.nextInt(900000);
//        System.out.println("生成的六位编号是: " + sixDigitNumber);

        // 生成一个UUID
        String uuid = UUID.randomUUID().toString();
        // 从UUID中提取数字部分
        String digits = uuid.replaceAll("[^0-9]", "");
        // 如果提取的数字不足六位，就继续生成新的UUID直到满足六位
        while (digits.length() < 6) {
            uuid = UUID.randomUUID().toString();
            digits = digits + uuid.replaceAll("[^0-9]", "");
        }
        // 取前六位数字
        String sixDigitNumber = digits.substring(0, 6);
        // 打印六位数字
        System.out.println("Generated 6-digit number: " + sixDigitNumber);
        furniture.setNumber(Integer.parseInt(sixDigitNumber));
//        furniture.setNumber(Integer.valueOf(sixDigitNumber));
        furnitureMapper.insert(furniture);

    }

    @Override
    public void update(Furniture furniture) {
        furnitureMapper.updateById(furniture);
    }

    @Override
    public void updateAvatar(String avatarUrl, Integer id) {
        UpdateWrapper<Furniture> furnitureUpdateWrapper = new UpdateWrapper<>();
        furnitureUpdateWrapper.eq("id", id);
        furnitureUpdateWrapper.set("furniture_photo", avatarUrl);
        furnitureMapper.update(furnitureUpdateWrapper);
    }


    @Override
    public Boolean selectFurnitureName(String furnitureName, Integer id) {
        UpdateWrapper<Furniture> furnitureUpdateWrapper = new UpdateWrapper<>();
        furnitureUpdateWrapper.eq("id", id);
        furnitureUpdateWrapper.eq("furniture_name", furnitureName);
        Furniture furniture = furnitureMapper.selectOne(furnitureUpdateWrapper);
        if (furniture != null) {
            return true;
        } else {
            return false;
        }

    }


    @Override
    public Furniture selectNumber(Integer number) {
        UpdateWrapper<Furniture> furnitureUpdateWrapper = new UpdateWrapper<>();
        furnitureUpdateWrapper.eq("number", number);
        Furniture furniture = furnitureMapper.selectOne(furnitureUpdateWrapper);
       return furniture;
    }

    @Override
    public Page<Furniture> serchFurniture(Integer number, String furnitureName, String furniturePhoto, Integer currentPage, Integer pageSize,
                                          Integer furnitureTypesId, Integer quyuTypesId) {
        Page<Furniture> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        if (number != null && !"".equals(number)) {
            queryWrapper.like("number", number);
        }
        if (furnitureName != null && !"".equals(furnitureName)) {
            queryWrapper.like("furniture_name", furnitureName);
        }
        if (furniturePhoto != null && !"".equals(furniturePhoto)) {
            queryWrapper.like("furniture_photo", furniturePhoto);
        }
        if (furnitureTypesId != null && !"".equals(furnitureTypesId)) {
            queryWrapper.eq("furniture_types_id", furnitureTypesId);
        }
        if (quyuTypesId != null && !"".equals(quyuTypesId)) {
            queryWrapper.eq("quyu_types_id", quyuTypesId);
        }
        Page<Furniture> furniturePage = furnitureMapper.selectPage(page, queryWrapper);
        List<Furniture> records = furniturePage.getRecords();
        for (Furniture record : records) {
            Quyutypes quyutypes = quyutypesMapper.selectById(record.getQuyuTypesId());
            record.setQuyuTypes(quyutypes.getName());
            Furnituretypes furnituretypes = furnituretypesMapper.selectById(record.getFurnitureTypesId());
            record.setFurnitureTypes(furnituretypes.getName());
            System.out.println(quyutypes + ":" + furnituretypes + "sssssssssssssssssssss");

        }
        return furniturePage;
    }

}
